<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04-快递单号查询</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .search {
            position: relative;
            width: 178px;
            margin: 100px;
        }

        .con {
            /* 先隐藏 */
            display: none;
            position: absolute;
            top: -40px;
            width: 171px;
            border: 1px solid rgba(0, 0, 0, .2);
            box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
            padding: 5px 0;
            font-size: 18px;
            line-height: 20px;
            color: #333;
        }

        .con::before {
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            top: 0px;
            left: 18px;
            border: 8px solid #000;
            border-style: solid dashed dashed;
            border-color: #fff transparent transparent;
        }

        input {
            margin-top: 100px;
        }
    </style>
</head>
<body>
    <div class="search">
        <div class="con" style="width: 800px; height: 100px; font-size: 80px; background-color: skyblue;">默认内容</div>
        <input type="text" placeholder="请输入您的快递单号" class="jd"
            style="width: 800px; height: 60px; top:200px; font-size: 50px;">
    </div>
        
        <script>
            /* 
            需求：
                1. input 获取焦点，如果有内容，把 input 的值放入div并显示
                如果input的值为空，则div隐藏
                2. input 失去焦点，div隐藏
                3. 表单输入文字，则div显示
            注意: keydown keypress 事件触发 文字还没有落入文本框
            */

            let input = document.querySelector('input');
            let div = document.querySelector('.con');
            // 表单获取焦点
            input.addEventListener('focus',function(){
                let value = this.value;
                value = value.trim();
                if(value == ''){
                    div.style.display = 'none';
                } else{
                    div.style.display = 'block';
                    div.innerHTML = value;
                }
            });
            // 表单失去焦点
            input.addEventListener('blur',function(){
                div.style.display = 'none';
            });
            // 输入文字
            input.addEventListener('keyup',function(){
                let value = this.value;
                value = value.trim();
                if(value != ''){
                    div.innerHTML = value;
                    div.style.display = 'block';
                }else{
                    div.style.display = 'none';
                }
            });
        </script>
</body>
</html>